/**
 * 
 */
package com.nextive.interview.activity;

import com.nextive.interview.R;
import com.nextive.interview.dao.WeightInfoDao;
import com.nextive.interview.service.WeightInfoService;
import com.nextive.interview.service.impl.WeightInfoServiceImpl;

import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.AdapterView.AdapterContextMenuInfo;

/**
 * @author Martin
 *
 */
public class ViewWeightListActivity extends ListActivity {

	private WeightInfoService userInfoService;
	
	private static final int ACTIVITY_EDIT = 0;
	private static final int ACTIVITY_CREATE = 1;
	private static final int DELETE_ID = Menu.FIRST;
	private static final int CREATE_ID = Menu.FIRST + 1;
	
	
	public ViewWeightListActivity() {
		
		this.userInfoService = new WeightInfoServiceImpl(this);
		
	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
		setContentView(R.layout.weight_list);
		
		populateWeightInfo();
		registerForContextMenu(getListView());
		
	}
	
	@Override
	public boolean onContextItemSelected(MenuItem item) {
		
		switch (item.getItemId()) {
		case DELETE_ID:
			AdapterContextMenuInfo menuInfo = (AdapterContextMenuInfo) item.getMenuInfo();
			userInfoService.deleteWeightInfo(menuInfo.id);
			populateWeightInfo();
			return true;
		}
		
		return super.onContextItemSelected(item);
		
	}

	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
		
		super.onCreateContextMenu(menu, v, menuInfo);
		menu.add(0, DELETE_ID, 0, R.string.delete_weight_row);
		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		
		super.onCreateOptionsMenu(menu);
		menu.add(0, CREATE_ID, 0, R.string.new_weight_row);
		return true;
		
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		
		switch (item.getItemId()) {
		case CREATE_ID:
			forwardToCreateActivity();
			return true;
		}
		
		return super.onOptionsItemSelected(item);
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		
		super.onActivityResult(requestCode, resultCode, data);
		populateWeightInfo();
		
	}

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		
		super.onListItemClick(l, v, position, id);
		
		Intent intent = new Intent(this, EditWeightEntryActivity.class);
		intent.putExtra(WeightInfoDao.ID_KEY, id);
		
		startActivityForResult(intent, ACTIVITY_EDIT);
		
	}

	private void populateWeightInfo() {
		
		Cursor weightInfoCursor = userInfoService.generateViewAllWeightHistoryCursor();
		startManagingCursor(weightInfoCursor);
		
		String[] modelColumns = new String[] {WeightInfoDao.WEIGHT_KEY};
		int[] viewColumns = new int[] {R.id.text1};
		
		SimpleCursorAdapter weightListAdapter = new SimpleCursorAdapter(this, R.layout.weight_row, 
				weightInfoCursor, modelColumns, viewColumns);
		setListAdapter(weightListAdapter);
		
	}
	
	private void forwardToCreateActivity() {
		
		Intent intent = new Intent(this, EditWeightEntryActivity.class);
		startActivityForResult(intent, ACTIVITY_CREATE);
		
	}
	

}
